
package codeGenerate.powerdesigner.generater;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.metadata.WriteSheet;

import codeGenerate.powerdesigner.converter.PowerdesignerConverter;
import codeGenerate.powerdesigner.entry.PowerdesignerConfigVo;
import codeGenerate.powerdesigner.entry.PowerdesignerTableColumnVo;

public class PowerdesignerGenerater {

	private PowerdesignerConfigVo configVo;

	private List<PowerdesignerTableColumnVo> tableFieldList = new ArrayList<PowerdesignerTableColumnVo>();

	public void generateCode() {
		System.out.println(">>>>>>>>>>>>>>>>>>>生成开始<<<<<<<<<<<<<<<<<<<");
		System.out.println(configVo);
		createTableField();
		validateTableField();
		try {
			createExcel();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>生成结束<<<<<<<<<<<<<<<<<<<");
		System.out.println("\n");
	}

	public void createTableField() {
		try {
			Class<?> converterClass = Class.forName(configVo.getConverter());
			PowerdesignerConverter converter = (PowerdesignerConverter) converterClass.newInstance();

			for (String orignName : orignNameList()) {
				PowerdesignerTableColumnVo tableColumn = converter.convert(orignName);
				tableFieldList.add(tableColumn);
				System.out.println(">>>>>>>>>>生成字段：" + tableColumn);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 验证表字段项，是否重复
	 */
	public void validateTableField() {
		Set<String> orignNameSet = new HashSet<>();
		Set<String> targetCodeSet = new HashSet<>();
		for (PowerdesignerTableColumnVo tableField : tableFieldList) {
			if (orignNameSet.contains(tableField.getOrignName())) {
				throw new RuntimeException(String.format("名称存在重复，名称为：%s", tableField.getOrignName()));
			}
			if (targetCodeSet.contains(tableField.getCode())) {
				throw new RuntimeException(String.format("自动生成的字段编码存在重复,字段编码：%s，对应字段名称：%s", tableField.getCode(), tableField.getOrignName()));
			}
			orignNameSet.add(tableField.getOrignName());
			targetCodeSet.add(tableField.getCode());
		}
	}

	/**
	 * 生成excel
	 */
	public void createExcel() throws FileNotFoundException {
		File file = new File(configVo.getGenPath() + File.separator + configVo.getTableCode() + ".xlsx");
		file.getParentFile().mkdirs();
		ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(new FileInputStream(configVo.getExcelTemplatePath())).build();
		WriteSheet writeSheet = EasyExcel.writerSheet().build();
		excelWriter.fill(tableFieldList, writeSheet);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tableCode", configVo.getTableCode());
		map.put("tableComment", configVo.getTableComment());
		excelWriter.fill(map, writeSheet);
		excelWriter.finish();
	}

	/**
	 * 获取原始名称
	 * @author fudewei
	 * @date 2023-02-23
	 * @return
	 */
	public List<String> orignNameList() {
		List<String> orignNameList = new ArrayList<>();
		String[] columnlines = configVo.getFieldContent().trim().split("\n");
		for (String string : columnlines) {
			if (StringUtils.isNotBlank(string)) {
				orignNameList.add(string.trim());
			}
		}
		return orignNameList;
	}

	public PowerdesignerConfigVo getConfigVo() {
		return configVo;
	}

	public void setConfigVo(PowerdesignerConfigVo configVo) {
		this.configVo = configVo;
	}
}
